package example.servlet;


import example.pojo.SysRole;
import example.pojo.SysUser;
import example.service.RoleService;
import example.service.RoleServiceImpl;
import example.service.UserService;
import example.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/role")
public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    private UserService userService = new UserServiceImpl();
    private PrintWriter out;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码方式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text;html;charset=utf-8");
        String opr = req.getParameter("opr");
        if(opr.equals("list")){
            List<SysRole> roleList = roleService.roleList();
            if(roleList!=null){
                req.getSession().setAttribute("roleList",roleList);
                req.getRequestDispatcher("/WEB-INF/jsp/sysRole/list.jsp").forward(req,resp);
            }else{
                System.out.println("角色信息获取失败");
                req.getRequestDispatcher("/WEB-INF/jsp/frame.jsp").forward(req,resp);
            }
        }else if(opr.equals("toAdd")){
            req.getRequestDispatcher("/WEB-INF/jsp/sysRole/add.jsp").forward(req,resp);
        }else if(opr.equals("toUpdate")){
            String rid = req.getParameter("rid");
            SysRole role = roleService.selectRoleByRid(rid==null||rid.equals("")?null:Integer.parseInt(rid));
            req.setAttribute("sysRole",role);
            req.getRequestDispatcher("/WEB-INF/jsp/sysRole/update.jsp").forward(req,resp);
        }else if(opr.equals("del")){
            del(req,resp);

        }else if(opr.equals("add")){
            String code = req.getParameter("code");
            String roleName = req.getParameter("roleName");
            SysRole role = new SysRole();
            role.setCode(code);
            role.setRoleName(roleName);
            int count = roleService.add(role);
            if(count>0){
                System.out.println("添加成功");
                req.getRequestDispatcher("role?opr=list").forward(req,resp);
            }else{
                System.out.println("添加失败");
                req.getRequestDispatcher("/WEB-INF/jsp/sysRole/list.jsp").forward(req,resp);
            }
        }else if(opr.equals("codeExist")){
            out = resp.getWriter();
            switch (roleService.codeExist(req.getParameter("code"))){
                case 1:
                    out.println(1);
                    break;
                case 0:
                    out.println(0);
                    break;
                default:
                    out.println(-1);
                    break;
            }
        }else if(opr.equals("update")){
            update(req,resp);
        }
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException ,ServletException{
        out = resp.getWriter();
        int count = 0;
        String rid = req.getParameter("rid");
        List<SysUser> userList = userService.getUserBindRoleId(Integer.parseInt(rid));
        if(userList!=null){
            //用户中有对应信息
            int userCount = userService.delUserByRoleId(Integer.parseInt(rid));
            count = roleService.del(rid==null||rid.equals("")?null:Integer.parseInt(rid));
            System.out.println(userCount);
        }else{
            count = roleService.del(rid==null||rid.equals("")?null:Integer.parseInt(rid));
        }
        if(count>0){
            out.println(true);
        }else{
            out.println(false);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String code = req.getParameter("code");
        String roleName = req.getParameter("roleName");
        int id = Integer.parseInt(req.getParameter("id"));
        SysRole role = new SysRole();
        role.setCode(code);
        role.setRoleName(roleName);
        role.setId(id);
        if(roleService.upd(role) > 0){
            System.out.println("角色信息修改成功");
            req.getRequestDispatcher("role?opr=list").forward(req,resp);
        }else{
            System.out.println("角色信息修改失败");
            req.getRequestDispatcher("/WEB-INF/jsp/sysRole/list.jsp").forward(req,resp);
        }
    }
}
